package com.xjh.him.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.him.common.LoginUser;
import com.xjh.him.constant.Constant;
import com.xjh.him.dao.FriendGroupMapper;
import com.xjh.him.dao.FriendGroupMsgMapper;
import com.xjh.him.dao.UserMapper;
import com.xjh.him.domain.FriendGroupDO;
import com.xjh.him.domain.FriendGroupMsgDO;
import com.xjh.him.domain.UserDO;
import com.xjh.him.dto.friendgroup.FriendGroupDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.ResultDTO;

import java.util.ArrayList;
import java.util.List;

@Service
public class FriendGroupServiceImpl implements IFriendGroupService{

    @Autowired
    private FriendGroupMapper friendGroupMapper;

    @Autowired
    private FriendGroupMsgMapper friendGroupMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultDTO<Long> queryOrAddGroup(Long firstId, Long secondId) {
        if (Checks.isAnyEmpty(firstId, secondId)) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        QueryWrapper<FriendGroupDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("first_id", Math.min(firstId, secondId));
        queryWrapper.eq("second_id", Math.max(firstId, secondId));
        FriendGroupDO friendGroupDO = friendGroupMapper.selectOne(queryWrapper);
        if (friendGroupDO == null) {
            friendGroupDO = new FriendGroupDO();
            friendGroupDO.setFirstId(Math.min(firstId, secondId));
            friendGroupDO.setSecondId(Math.max(firstId, secondId));
            friendGroupMapper.insert(friendGroupDO);
        }
        return ResultDTO.getSuccessResult(friendGroupDO.getId());
    }

    @Override
    public ResultDTO<List<FriendGroupDTO>> listGroup() {
        UserDO loginUser = LoginUser.getLoginUser();
        List<FriendGroupDO> groupDOList = friendGroupMapper.queryGroup(loginUser.getId(), Constant.DEFAULT_LIST_GROUP_SIZE);
        List<FriendGroupDTO> dtoList = new ArrayList<>();
        for (FriendGroupDO groupDO : groupDOList) {
            FriendGroupDTO dto = new FriendGroupDTO();
            dtoList.add(dto);
            BeanUtils.copyProperties(groupDO, dto);

            Long memberId = loginUser.getId().equals(groupDO.getFirstId()) ?
                    groupDO.getSecondId() : groupDO.getFirstId();
            UserDO userDO = userMapper.selectById(memberId);
            dto.setFriendId(memberId);
            dto.setGroupName(userDO.getNickname());
            dto.setGroupAvatar(userDO.getAvatarUrl());

            QueryWrapper<FriendGroupMsgDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("friend_group_id", groupDO.getId());
            queryWrapper.orderByDesc("id");
            queryWrapper.last("LIMIT 1");
            FriendGroupMsgDO groupMsgDO = friendGroupMsgMapper.selectOne(queryWrapper);
            if (groupMsgDO == null) {
                dto.setNewestMsg("");
                continue;
            }
            if (FriendGroupMsgDO.MSG_TYPE_TEXT.equals(groupMsgDO.getMsgType())) {
                dto.setNewestMsg(groupMsgDO.getMsgContent());
            }else if (FriendGroupMsgDO.MSG_TYPE_IMAGE.equals(groupMsgDO.getMsgType())) {
                dto.setNewestMsg("[图片]");
            }
        }
        return ResultDTO.getSuccessResult(dtoList);
    }
}
